package exercise;

import java.util.Random;
import java.util.Scanner;

public class Text7 {
    public static void main(String[] args) {
        // 目标：双色球开发。
        int[] userNumber = userSelectNumbers();
        System.out.println("您投注的号码:");
        printArray(userNumber);

        int[] luckNumbers = creatLuckNumbers();
        System.out.println("您中奖的号码:");
        printArray(luckNumbers);

        judge(userNumber, luckNumbers);
    }

    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
        }
        System.out.println("]");
    }

    public static int[] userSelectNumbers() {
        int[] numbers = new int[7];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < numbers.length - 1; i++) {
            while (true) {
                System.out.println("请输入第" + (i + 1) + "个红球号码(1-33)之间，不能重复");
                int number = sc.nextInt();
                if (number < 1 || number > 33) {
                    System.out.println("输入错误");
                } else {
                    if (exist(numbers, number)) {
                        System.out.println("错误:红球号码重复");
                    } else {
                        numbers[i] = number;
                        break;
                    }

                }
            }
        }
        System.out.println("请输入最后一个蓝球号码:");
        int number = sc.nextInt();
        while (true) {
            if (number < 1 || number > 16) {
                System.out.println("输入错误");
            } else {
                numbers[6] = number;
                break;
            }
        }
        return numbers;
    }

    private static boolean exist(int[] numbers, int number) {
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == 0) {
                break;
            }
            if (numbers[i] == number) {
                return true;
            }
        }
        return false;
    }

    public static int[] creatLuckNumbers() {
        int[] numbers = new int[7];
        Random r = new Random();
        for (int i = 0; i < numbers.length - 1; i++) {
            while (true) {
                int number = r.nextInt(33) + 1;
                if (!exist(numbers, number)) {
                    numbers[i] = number;
                    break;
                }
            }
        }
        numbers[6] = r.nextInt(16) + 1;
        return numbers;
    }

    public static void judge(int[] userNumbers, int[] luckNumbers) {
        int redCount = 0;
        int blueCount = 0;
        for (int i = 0; i < userNumbers.length - 1; i++) {
            for (int j = 0; j < luckNumbers.length - 1; j++) {
                if (userNumbers[i] == luckNumbers[j]) {
                    redCount++;
                    break;
                }
            }
        }
        blueCount = userNumbers[6] == luckNumbers[6] ? 1 : 0;

        System.out.println("命中红球数量是:" + redCount);
        System.out.println("命中篮球数量是:" + blueCount);

        if (redCount == 6 && blueCount == 1) {
            System.out.println("中奖1000w元");
        } else if (redCount == 6 && blueCount == 0) {
            System.out.println("中奖500w元");
        } else if (redCount == 5 && blueCount == 1) {
            System.out.println("中奖3000元");
        } else if (redCount == 5 && blueCount == 0 || redCount == 4 && blueCount == 1) {
            System.out.println("中奖200元");
        } else if (redCount == 4 && blueCount == 0 || redCount == 3 && blueCount == 1) {
            System.out.println("中奖10元");
        } else if (redCount < 3 && blueCount == 1) {
            System.out.println("中奖5元");
        } else {
            System.out.println("没中奖");
        }
    }
}
